package com.acetech.admin.web.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class ResultData<T> implements Serializable {
	/**
	 *
	 */
	private static final long serialVersionUID = 6616825653919662101L;
	private Integer code;
	private String msg;
	private Object data;

	public ResultData() {
	}

	public ResultData(ErrorCode errorCode, Object data) {
		this.code = errorCode.code;
		this.msg = errorCode.msg;
		this.data = data;
	}

	public ResultData(Integer code, String msg, Object data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	public static ResultData success() {
		return new ResultData(ErrorCode.SUCCESS, null);
	}

	public static ResultData success(Object data) {
		return new ResultData(ErrorCode.SUCCESS, data);
	}

	public static ResultData error(String msg) {
		return new ResultData(ErrorCode.BUSI_ERROR.code, msg, null);
	}

	public static ResultData error(int code, String msg) {
		return new ResultData(code, msg, null);
	}

	public static ResultData error(ErrorCode errorCode) {
		return new ResultData(errorCode, null);
	}

	public static ResultData sysError() {
		return new ResultData(ErrorCode.SYS_ERROR);
	}
	public static ResultData busiError(String msg) {
		return new ResultData(ErrorCode.BUSI_ERROR.code, msg, null);
	}

	public static ResultData paramsError() {
		return new ResultData(ErrorCode.PARAMS_ERROR);
	}
	public static ResultData paramsError(String msg) {
		return new ResultData(ErrorCode.PARAMS_ERROR.code, msg, null);
	}

	public static ResultData unknownError() {
		return new ResultData(ErrorCode.UNKNOWN_ERROR);
	}
	public static ResultData netError() {
		return new ResultData(ErrorCode.NET_ERROR);
	}

	public ResultData(ErrorCode errorCode) {
		this.code = errorCode.code;
		this.msg = errorCode.msg;
	}

	public static ResultData redirect(String routerUrl) {
		return redirect(routerUrl, false);
	}

	public static ResultData redirect(String routerUrl, boolean releaseView) {
		Map<String, Object> json = new HashMap<>();
		String joinStr = "?";
		if (routerUrl.indexOf("?") != -1) {
			joinStr = "&";
		}
		json.put("url", routerUrl + (releaseView ? joinStr + "releaseView=1" : ""));
		return new ResultData(ErrorCode.REDIRECT_TEMP.code, null, json);
	}

    public static ResultData needGraphCode() {
		return new ResultData(ErrorCode.NEED_GRAPH_CODE);
    }

	public static ResultData errorGraphCode() {
		return new ResultData(ErrorCode.ERROR_GRAPH_CODE);
	}
}
